package luastring

import (
	"strings"
	"time"

	lua "github.com/yuin/gopher-lua"
	"golang.org/x/text/encoding/simplifiedchinese"
)

//HasPrefix 是否有前缀
func HasPrefix(l *lua.LState) int {
	n := l.GetTop()
	if n < 2 {
		l.Push(lua.LString(""))
		return 1
	}
	s := l.CheckString(1)
	prefix := l.CheckString(2)
	l.Push(lua.LBool(strings.HasPrefix(s, prefix)))
	return 1
}

//HasSuffix 是否有后缀
func HasSuffix(l *lua.LState) int {
	n := l.GetTop()
	if n < 2 {
		l.Push(lua.LString(""))
		return 1
	}
	s := l.CheckString(1)
	suffix := l.CheckString(2)
	l.Push(lua.LBool(strings.HasSuffix(s, suffix)))
	return 1
}

//TrimPrefix 删除前缀
func TrimPrefix(l *lua.LState) int {
	n := l.GetTop()
	if n < 2 {
		l.Push(lua.LString(""))
		return 1
	}
	s := l.CheckString(1)
	prefix := l.CheckString(2)
	l.Push(lua.LString(strings.TrimPrefix(s, prefix)))
	return 1
}

//TrimSuffix 删除后缀
func TrimSuffix(l *lua.LState) int {
	n := l.GetTop()
	if n < 2 {
		l.Push(lua.LString(""))
		return 1
	}
	s := l.CheckString(1)
	suffix := l.CheckString(2)
	l.Push(lua.LString(strings.TrimSuffix(s, suffix)))
	return 1
}

//TrimRight 从右删除
func TrimRight(l *lua.LState) int {
	n := l.GetTop()
	if n < 2 {
		l.Push(lua.LString(""))
		return 1
	}
	s := l.CheckString(1)
	prefix := l.CheckString(2)
	l.Push(lua.LString(strings.TrimRight(s, prefix)))
	return 1
}

//TrimLeft 从左删除
func TrimLeft(l *lua.LState) int {
	n := l.GetTop()
	if n < 2 {
		l.Push(lua.LString(""))
		return 1
	}
	s := l.CheckString(1)
	suffix := l.CheckString(2)
	l.Push(lua.LString(strings.TrimLeft(s, suffix)))
	return 1
}

//TrimSpace 删除两边空格
func TrimSpace(l *lua.LState) int {
	n := l.GetTop()
	if n < 1 {
		l.Push(lua.LString(""))
		return 1
	}
	l.Push(lua.LString(strings.TrimSpace(l.CheckString(1))))
	return 1
}

//Fields 按空格分割
func Fields(l *lua.LState) int {
	delim := rune(0)
	n := l.GetTop()
	if n < 1 {
		return 0
	}
	line := l.CheckString(1)
	if line == "" {
		return 0
	}
	if n > 1 {
		sep := l.CheckString(2)
		if sep == "" {
			return 0
		}
		delim = rune(sep[0])
	}
	ss := strings.FieldsFunc(line, func(r rune) bool {
		if delim == 0 {
			if r == '\r' || r == '\n' || r == '\t' || r == ' ' {
				return true
			}
			return false
		}
		if r == delim {
			return true
		}
		return false
	})

	tab := l.NewTable()
	for i, s := range ss {
		tab.Insert(i+1, lua.LString(s))
	}
	l.Push(tab)
	return 1
}

//Split 按给定的字符串分割
func Split(l *lua.LState) int {
	delim := ","
	n := l.GetTop()
	if n < 1 {
		return 0
	}
	line := l.CheckString(1)
	if line == "" {
		return 0
	}
	if n > 1 {
		delim = l.CheckString(2)
		if delim == "" {
			return 0
		}
	}
	ss := strings.Split(line, delim)

	tab := l.NewTable()
	for i, s := range ss {
		tab.Insert(i+1, lua.LString(s))
	}
	l.Push(tab)
	return 1
}

//Sleep 睡眠 毫秒
func Sleep(l *lua.LState) int {
	ms := l.ToInt64(1)
	time.Sleep(time.Millisecond * time.Duration(ms))
	return 0
}

//Utf8 转换utf8 编码
func Utf8(l *lua.LState) int {
	n := l.GetTop()
	if n < 1 {
		l.Push(lua.LString(""))
		return 1
	}

	decoder := simplifiedchinese.GBK.NewDecoder()
	data, _ := decoder.String(l.CheckString(1))
	l.Push(lua.LString(data))
	return 1
}

//Ansi 转换ansi编码
func Ansi(l *lua.LState) int {
	n := l.GetTop()
	if n < 1 {
		l.Push(lua.LString(""))
		return 1
	}
	encoder := simplifiedchinese.GBK.NewEncoder()
	data, _ := encoder.String(l.CheckString(1))
	l.Push(lua.LString(data))
	return 1
}
